package com.coocaa.ops.admin.v1.timedtask.service.impl;

import com.coocaa.ops.admin.v1.timedtask.constant.TimedTaskTypeConstants;
import com.coocaa.ops.admin.v1.timedtask.job.impl.MyQuartzJobServiceImpl;
import com.coocaa.ops.admin.v1.timedtask.provider.impl.TimedTaskProviderImpl;
import com.coocaa.ops.admin.v1.timedtask.service.ITimedTaskUpdateService;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author bijiahao
 * @date : 2019/8/7.
 * @description 定时信息 更新操作 服务实现
 *
 */
@Service
public class TimedTaskUpdateServiceImpl implements ITimedTaskUpdateService {
    @Autowired
    private TimedTaskProviderImpl timedTaskProvider;
    @Autowired
    private MyQuartzJobServiceImpl myQuartzJobService;

    @Override
    public R saveJob(TimedTaskInfoEntity entity) {
        if (TimedTaskTypeConstants.isTimelyhelving(entity.getTimedType())) {
            return R.fail("定时上架不支持创建Job");
        }
        return R.fail("当前不支持！");
    }

    @Override
    public R resumeJob(Integer id) {
        TimedTaskInfoEntity entity = timedTaskProvider.getTimedTaskBiz().queryById(id);
        if (null == entity) {
            return R.fail("定时任务信息为空！");
        }
        if (TimedTaskTypeConstants.isTimelyhelving(entity.getTimedType())) {
            return R.fail("定时上架不支持恢复Job");
        }
        return R.fail("当前不支持！");
    }

    @Override
    public R stopJob(Integer id) {
        TimedTaskInfoEntity entity = timedTaskProvider.getTimedTaskBiz().queryById(id);
        if (null == entity) {
            return R.fail("定时任务信息为空！");
        }
        if (TimedTaskTypeConstants.isRecommendPoolUpdate(entity.getTimedType())) {
            boolean isJobExist = myQuartzJobService.isJobExist(entity);
            if (isJobExist) {
                myQuartzJobService.delJob(entity);
            }
            return R.ok();
        }
        if (TimedTaskTypeConstants.isTimelyhelving(entity.getTimedType())) {
            return R.fail("定时上架不支持停止Job");
        }
        return R.fail("当前不支持！");
    }

    @Override
    public R delJob(Integer id) {
        TimedTaskInfoEntity entity = timedTaskProvider.getTimedTaskBiz().queryById(id);
        if (null == entity) {
            return R.fail("定时任务信息为空！");
        }
        if (TimedTaskTypeConstants.isRecommendPoolUpdate(entity.getTimedType())) {
            myQuartzJobService.delJob(entity);
            timedTaskProvider.deleteById(entity.getId());
            return R.ok();
        }
        if (TimedTaskTypeConstants.isTimelyhelving(entity.getTimedType())) {
            return R.fail("定时上架不支持删除Job");
        }
        return R.fail("当前不支持！");
    }
}
